/*
 * ProgressBarDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "ProgressBarDelegate.h"

// ProgressBarDelegate :: Protected Constructor. -------------------------------

/*!
 * \brief Constructs new ProgressBarDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj ProgressBar java object
 */
ProgressBarDelegate::ProgressBarDelegate(JNIEnv *env, jobject obj)
  : ComponentDelegate(env, obj, new QProgressBar())
{
  qProgressBar()->setAlignment(Qt::AlignCenter);
  qProgressBar()->setTextVisible(false);
}

// ProgressBarDelegate :: Native Calls. ----------------------------------------

/*!
 * \brief Sets model.
 *
 * \param env the JNIEnv pointer
 * \param minimum the minimum progress value
 * \param maximum the maximum progress value
 * \param value the progress value
 */
void ProgressBarDelegate::setModel(JNIEnv *env, jint minimum, jint maximum,
                                   jint value)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setModel, SWING_ENV_ARG, JINT_ARG(minimum),
                  JINT_ARG(maximum), JINT_ARG(value))
  qProgressBar()->setMinimum(minimum);
  qProgressBar()->setMaximum(maximum);
  qProgressBar()->setValue(value);
}

/*!
 * \brief Sets progress value.
 *
 * \param env the JNIEnv pointer
 * \param value the progress value
 */
void ProgressBarDelegate::setValue(JNIEnv *env, jint value)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setValue, SWING_ENV_ARG, JINT_ARG(value))
  qProgressBar()->setValue(value);
}

/*!
 * \brief Sets minimum progress value.
 *
 * \param env the JNIEnv pointer
 * \param minimum the minimum progress value
 */
void ProgressBarDelegate::setMinimum(JNIEnv *env, jint minimum)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setMinimum, SWING_ENV_ARG, JINT_ARG(minimum))
  qProgressBar()->setMinimum(minimum);
}

/*!
 * \brief Sets maximum progress value.
 *
 * \param env the JNIEnv pointer
 * \param maximum the maximum progress value
 */
void ProgressBarDelegate::setMaximum(JNIEnv *env, jint maximum)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setMaximum, SWING_ENV_ARG, JINT_ARG(maximum))
  qProgressBar()->setMaximum(maximum);
}

/*!
 * \brief Sets progress bar orientation.
 *
 * \param env the JNIEnv pointer
 * \param orientation the orientation
 */
void ProgressBarDelegate::setOrientation(JNIEnv *env, jint orientation)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setOrientation, SWING_ENV_ARG, JINT_ARG(orientation))
  switch(orientation) {
    case 0:
      qProgressBar()->setOrientation(Qt::Horizontal);
      break;
    case 1:
      qProgressBar()->setOrientation(Qt::Vertical);
      break;
  }
}

/*!
 * \brief Sets progress string.
 *
 * \param env the JNIEnv pointer
 * \param progressString the progress string
 */
void ProgressBarDelegate::setProgressString(JNIEnv *env, jstring progressString)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setProgressString, SWING_ENV_ARG,
                  JSTRING_ARG(progressString))
  if (progressString) {
   qProgressBar()->setFormat(QStringAdapter(env, progressString));
  } else {
   qProgressBar()->setFormat("%p%");
  }
}

/*!
 * \brief Set whether or not the progress string is painted.
 *
 * \param env the JNIEnv pointer
 * \param paintString \c true if painted
 */
void ProgressBarDelegate::setStringPainted(JNIEnv *env, jboolean paintString)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setStringPainted, SWING_ENV_ARG, JBOOLEAN_ARG(paintString))
  qProgressBar()->setTextVisible(paintString);
}

// ProgressBarDelegate :: Static Native Calls. ---------------------------------

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong ProgressBarDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new ProgressBarDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}
